#!/usr/bin/env perl
#
# $Id: netdevice.pl acabbce13f25 2012/11/11 02:33:52 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# 
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#       
# __version__  = "$Revision: acabbce13f25 $"
# __author__   = "$Author: pgurumur $"
# __modified__ = "$Date: 2012-11-11 02:33:52Z $"


use strict;
use warnings;
use Getopt::Long;
use Net::SNMP;
use Config::IniFiles;
use DBD::mysql;

Getopt::Long::Configure("default", "bundling");
push my @prog, split(/\//, $0);
my $program = $prog[$#prog];

my $arg = int(scalar(@ARGV));
my $session = undef;
my $error = undef;

sub sighandle {
   die "$program got interrupt signal, terminating the application\n";
}

sub errx {
   my ($state, $message) = @_;

   if ($state eq -1) {
      printf("UNKNOWN: %s\n", $message);
   }
   elsif ($state eq 0) {
      printf("OK");
   }
   elsif ($state eq 1) {
      printf("WARNING: %s\n", $message);
   }
   elsif ($state eq 2) {
      printf("CRITICAL: %s\n", $message);
   }

   if (defined($session)) {
      $session->close;
   }

   exit($state);
}

sub service_check {
   my ($monitor, $oid) = @_;

   if ($monitor == "memory") {
      my $freemem = $session->get_request(-varbindlist => [$oid]);
      my $free = $freemem->{$oid};

      if ($free lt 75) {
         errx(0, "memory is okay");
      }
      elsif (($free lt 85) && ($free gt 75)) {
         errx(1, "memory too high");
      }
      elsif ($free gt 85) {
         errx(2, "memory is critical");
      }
      elsif ($free lt 0) {
         errx(-1, "memory query error");
      }
   }
   elsif ($monitor == "cpu") {
      my $cpufree = $session->get_request(-varbindlist => [$oid]);
      my $free = $cpufree->{$oid};

      if ($free lt 0) {
         errx(-1, "cpu query error");
      }
      elsif ($free lt 75) {
         errx(0, "cpu is okay");
      }
      elsif (($free lt 85) && ($free gt 75)){
         errx(1, "cpu too high");
      }
      elsif ($free gt 85) {
         errx(2, "cpu is critical");
      }
   }
}

sub fatal {
   printf("%s %s\n", $program, @_);
   exit 1;
}

sub usage {
   printf("%s: options\n", $program);
   print "\t--help | -?         [ prints this help ]\n";
   print "\t--device | -d       [ type of device ]\n";
   print "\t--type | -t         [ type of nagios check ]\n";
   print "\t--host | -h         [ hostname of the device ]\n";
   print "\t--community | -c    [ SNMP v2/v1 community for the device ]\n";
   print "\t--all | -a          [ Check all devices ]\n";
   print "\t--config | -C       [ Configuration file for DB access ]\n";
   print "\n";
   print "\tList of options for monitor switch\n";
   print "\t\tmemory\n";
   print "\t\tcpu\n";
   exit (1);
}

sub main {
   GetOptions(my $opts = {},
      "help|?", # Prints this help
      "community|c=s", # SNMP R/O community string
      "device|d=s", # Type of device
      "type|t=s", # Type of nagios check
      "all|a",  # Check all devices based on netdot
      "config|C=s", # Configuration file for DB access
      "host|h=s", # Hostname (preferably IP address)
      ) or usage();
   usage() if $opts->{help};

   $SIG{INT} = \&sighandle;
   $SIG{TERM} = \&sighandle;

   my $dbh = undef;
   if ($opts->{config}) {
      my $cfg = Config::IniFiles->new(
         -file => $opts->{config},
         -default => "all" );
      my $dbname = $cfg->val("Netdot", "dbname");
      my $dbport = $cfg->val("Netdot", "dbport");
      my $dbhost = $cfg->val("Netdot", "dbhost");
      my $dbuser = $cfg->val("Netdot", "dbuser");
      my $dbpass = $cfg->val("Netdot", "dbpass");

      my $dsn = "DBI:mysql:database=$dbname;host=$dbhost;port=$dbport";
      $dbh = DBI->connect($dsn, $dbuser, $dbpass, { RaiseError => 1});
   }

   if ($opts->{all}) {
   }
   else {

}

if ($arg le 1) {
   usage();
}
else {
   main();
}
